import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisException;

public class TestRedisConnection {
    public static void main(String[] args) {
        System.out.println("Testing Redis connection...");
        
        // 测试无密码连接
        System.out.println("\n1. Testing connection without password:");
        testConnection(null);
        
        // 测试有密码连接
        System.out.println("\n2. Testing connection with password 'RedisData@123456.':");
        testConnection("RedisData@123456.");
        
        // 测试其他可能的密码
        System.out.println("\n3. Testing connection with empty password:");
        testConnection("");
    }
    
    private static void testConnection(String password) {
        Jedis jedis = null;
        try {
            jedis = new Jedis("127.0.0.1", 6379);
            
            if (password != null && !password.isEmpty()) {
                jedis.auth(password);
                System.out.println("✓ Authentication successful with password: " + password);
            } else {
                System.out.println("✓ Connecting without authentication...");
            }
            
            String result = jedis.ping();
            System.out.println("✓ PING response: " + result);
            
            // 测试设置和获取
            jedis.set("test_key", "test_value");
            String value = jedis.get("test_key");
            System.out.println("✓ SET/GET test successful: " + value);
            jedis.del("test_key");
            
        } catch (JedisException e) {
            System.out.println("✗ Redis connection failed: " + e.getMessage());
        } catch (Exception e) {
            System.out.println("✗ Unexpected error: " + e.getMessage());
        } finally {
            if (jedis != null) {
                try {
                    jedis.close();
                } catch (Exception e) {
                    // ignore
                }
            }
        }
    }
} 